import { mapState } from "vuex";
import webUrl from "@/config/otherLink";
export default {
  data() {
    return {
      loading: false,
      random: "",
    };
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.common.userInfo,
      financeId: (state) => state.companyInfo.financeId,
      uif: (state) => state.common.uif,
    }),
  },
  methods: {
    getguid() {
      //生成随机 GUID 数
      function S4() {
        return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
      }
      return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
    },

    setUrlParam(name, value) {
      var Days = 30;
      var exp = new Date();
      exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); //.91yikuaiji.com
      return "?" + name + "=" + value + "&expires=" + exp.toGMTString() + "&path=/";
    },

    Entertheaccount(accountid, subid, item, linkurl) {
      // 进入记账
      let params = {
        accountid,
      };
      if (item) {
        this.$set(item, "isConfirmLoading", true);
      }
      this.$api.accountPort.OpenFinancialUrl(params).then((res) => {
        let result = res.data.data;
        if (item) {
          this.$set(item, "isConfirmLoading", false);
        }
        let tokenParams = {};
        let urlstr = result.url;
        tokenParams.userName = this.userInfo.realname; // 真实姓名
        tokenParams.ZtID = accountid;
        tokenParams.companyId = this.userInfo.CompanyId; // 企业ID
        tokenParams.companyNumber = this.userInfo.Serial; // 企业号
        tokenParams.userId = this.userInfo.UserId; // 用户id
        tokenParams.cType = 1;
        tokenParams.sign = result.sign;
        tokenParams.customerId = subid; // 公司id
        // tokenParams.isPopularize = true; // 是否普及版财务云
        tokenParams.uif = this.uif;
        tokenParams.jurisdictionid = this.financeId; // 财务权限id
        tokenParams.smallType = this.userInfo.smallType; // 版本
        tokenParams.is_machine_accounts = result.isMachineAccounts; // 是否开通智能做账 bool类型
        tokenParams.tax_inspection_surpluscount = result.taxInspectionSurpluscount; //  财税检测盈余数 int类型
        tokenParams.psiid = result.psId; // 进销存的ID
        tokenParams.tax_number = result.taxNumber; // 税号
        tokenParams.fisfinance = result.FIsFinance; // 税号
        tokenParams.ztname = result.ztname; // 税号
        tokenParams.companylogo = this.userInfo.companylogo; // logo
        tokenParams.isChangeLogo = this.userInfo.isChangeLogo; // logo
        if (process.env.NODE_ENV == "checkTest" || process.env.NODE_ENV == "development") console.log(tokenParams, "tokenParams");
        urlstr += this.setUrlParam("tokenParams", escape(JSON.stringify(tokenParams)));

        window.open(urlstr);
        if (!item) {
          this.$router.push({ name: "cancelAccountContainer", query: { isGetData: true } });
        }
      })
        .catch(() => {
          if (item) {
            this.$set(item, "isConfirmLoading", false);
          }
        });
    },

    EntertheaccountPortal(accountid, subid, item, PortalName) {
      // 进入记账
      let params = {
        accountid,
      };
      if (item) {
        this.$set(item, "isConfirmLoading", true);
      }
      this.$api.accountPort.OpenFinancialUrl(params).then((res) => {
        let result = res.data.data;
        if (item) {
          this.$set(item, "isConfirmLoading", false);
        }
        let tokenParams = {};
        let urlstr = result.url;
        tokenParams.userName = this.userInfo.realname; // 真实姓名
        tokenParams.ZtID = accountid;
        tokenParams.companyId = this.userInfo.CompanyId; // 企业ID
        tokenParams.companyNumber = this.userInfo.Serial; // 企业号
        tokenParams.userId = this.userInfo.UserId; // 用户id
        tokenParams.cType = 1;
        tokenParams.sign = result.sign;
        tokenParams.customerId = subid; // 公司id
        // tokenParams.isPopularize = true; // 是否普及版财务云
        tokenParams.uif = this.uif;
        tokenParams.jurisdictionid = this.financeId; // 财务权限id
        tokenParams.smallType = this.userInfo.smallType; // 版本
        tokenParams.is_machine_accounts = result.isMachineAccounts; // 是否开通智能做账 bool类型
        tokenParams.tax_inspection_surpluscount = result.taxInspectionSurpluscount; //  财税检测盈余数 int类型
        tokenParams.psiid = result.psId; // 进销存的ID
        tokenParams.tax_number = result.taxNumber; // 税号
        tokenParams.fisfinance = result.FIsFinance; // 税号
        tokenParams.ztname = result.ztname; // 税号
        tokenParams.companylogo = this.userInfo.companylogo; // logo
        tokenParams.isChangeLogo = this.userInfo.isChangeLogo; // logo

        if (process.env.NODE_ENV == "checkTest" || process.env.NODE_ENV == "development") console.log(tokenParams, "tokenParams");
        urlstr += this.setUrlParam("tokenParams", escape(JSON.stringify(tokenParams)));
        urlstr += `&isPortal=true&portalSeal=${PortalName}`;
        window.open(urlstr);
        if (!item) {
          this.$router.push({ name: "cancelAccountContainer", query: { isGetData: true } });
        }
      })
        .catch(() => {
          if (item) {
            this.$set(item, "isConfirmLoading", false);
          }
        });
    },

    Oneclickimport() {
      // 一键式导入
      this.$confirm("若导入多年度的账套，请先导最新年限的，导入顺序案例：2019→2018→2017→2016。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            subid: this.subid,
          };
          this.$api.companyInfo.Detail(params).then((res) => {
            let Information = res.data.data;
            // Information.importid = this.guid();
            // Information.taxType = Number(Information.taxType);
            // Information.accountType = 2;
            let copyinformation = JSON.stringify({
              subid: Information.subid,
              companyid: this.userInfo.CompanyId,
              subName: Information.subName,
              abbreviationName: Information.abbreviationName,
              createid: this.userInfo.UserId,
              createName: Information.createName,
              importid: this.getguid(),
              accountType: 2,
              taxTypeName: Information.taxTypeName,
            });
            let name = escape(this.abbreviationName);
            let copyinformations = escape(copyinformation);
            let radom = Math.random() * 100;
            let baseurl = webUrl.importData;
            // if (process.env.NODE_ENV == "checkTest" || process.env.NODE_ENV == "development" || process.env.NODE_ENV == "pre") {
            //   baseurl = "http://import.91yikuaiji.com/ImportZT/ZTImport.htm";
            // } else if (process.env.NODE_ENV == "prod" || process.env.NODE_ENV == "newProd") {
            //   baseurl = "https://upgrade.yundaizhang.com.cn:8442/ImportZT/ZTImport.htm";
            // }
            this.iframeSrc = `${baseurl}?cname=${name}&userid=${this.userInfo.UserId}&dbkey=4&typeKey=4&radom=${radom}&accountjson=${copyinformations}`;
            this.Visiblenel = true;
          });
        })
        .catch(() => {});
    },
  },
  mounted() {},
};
